﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace 命题证明
{
    public partial class frmTrueTable : Form
    {
        public frmTrueTable()
        {
            InitializeComponent();
        }

        public ToolStripStatusLabel showValidate;
        string ConvertToString( bool value )
        {
            if ( value )
                return "1";
            return "0";
        }
        private void ShowValidate( ExpressionException p )
        {
            //if ( p == null )
            //{
            //    showValidate.Text = "";
            //}
            //else
            //{
            //    txtExpression.Select( p.Index, 1 );
            //    showValidate.Text = p.Message;
            //    XBC.ControlComputer.PalyAlarm();
            //}
        }

        private static void SetColumn( DataGridViewTextBoxColumn t )
        {
            t.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }

        private void btnOK_Click( object sender, EventArgs e )
        {
            string s = txtExpression.Text.ToLower();
            try
            {
                ValidateExpression.CheckValidate( s );


                Expression exp = new Expression( s );
                TableWork table = new TableWork( exp );

                dataGridView1.Rows.Clear();
                dataGridView1.Columns.Clear();
                DataGridViewTextBoxColumn colD = new DataGridViewTextBoxColumn();
                colD.HeaderText = "十进制数";
                SetColumn( colD );

                dataGridView1.Columns.Add( colD );
                for ( int i = 0; i < exp.Words.Count; i++ )
                {
                    DataGridViewTextBoxColumn t = new DataGridViewTextBoxColumn();
                    SetColumn( t );
                    t.HeaderText = exp.Words[i].ToString();
                    dataGridView1.Columns.Add( t );
                    DataGridViewTextBoxColumn rt = new DataGridViewTextBoxColumn();
                    SetColumn( rt );
                    t.HeaderText = "结果";
                    dataGridView1.Columns.Add( rt );
                }

                //for ( int i = 0; i < table.Middles[0].Count; i++ )
                //{
                //    DataGridViewTextBoxColumn t = new DataGridViewTextBoxColumn();
                //    SetColumn( t );
                //    t.HeaderText = exp.ConvertToMiddle( table.Middles[0][i].Node );
                //    dataGridView1.Columns.Add( t );
                //}

                //if ( table.Middles[0].Count == 0 )
                //{
                //    DataGridViewTextBoxColumn strTxt = new DataGridViewTextBoxColumn();
                //    SetColumn( strTxt );
                //    strTxt.HeaderText = exp.ToString();
                //    dataGridView1.Columns.Add( strTxt );
                //}

                StringBuilder or = new StringBuilder();
                StringBuilder orm = new StringBuilder();
                StringBuilder and = new StringBuilder();
                StringBuilder andM = new StringBuilder();
                for ( int i = 0; i < table.Records.Count; i++ )
                {
                    List<object> rows = new List<object>();
                    rows.Add( i.ToString() );
                    for ( int j = 0; j < table.Records[i].Binary.Count; j++ )
                    {
                        rows.Add( ConvertToString( table.Records[i].Binary[j] ) );
                    }
                    //for ( int j = 0; j < table.Middles[0].Count; j++ )
                    //{
                    //    rows.Add( ExpressionFactory.Convert( table.Middles[i][j].Value ) );
                    //}
                    rows.Add( ConvertToString( table.Records[i].Value ) );
                    if ( table.Records[i].Value )
                    {
                        orm.Append( "m" + i.ToString() );
                        orm.Append( MainChar.Or );
                        or.Append( "(" );
                        for ( int j = 0; j < table.Records[i].Binary.Count; ++j )
                        {
                            if ( !table.Records[i].Binary[j] )
                                or.Append( MainChar.Not );
                            or.Append( exp.Words[j] );
                            if ( j != table.Records[i].Binary.Count - 1 )
                                or.Append( MainChar.And );
                        }
                        or.Append( ")" );
                        or.Append( MainChar.Or );
                    }
                    else
                    {
                        andM.Append( "M" + i.ToString() );
                        andM.Append( MainChar.And );
                        and.Append( "(" );
                        for ( int j = 0; j < table.Records[i].Binary.Count; ++j )
                        {
                            if ( table.Records[i].Binary[j] )
                                and.Append( MainChar.Not );
                            and.Append( exp.Words[j] );
                            if ( j != table.Records[i].Binary.Count - 1 )
                                and.Append( MainChar.Or );
                        }
                        and.Append( ")" );
                        and.Append( MainChar.And );
                    }

                    dataGridView1.Rows.Add( rows.ToArray() );
                }

                for ( int i = 0; i < dataGridView1.Rows.Count; i++ )
                {
                    dataGridView1.Rows[i].Resizable = DataGridViewTriState.False;
                }
                orm.Remove( orm.Length - 1, 1 );
                or.Remove( or.Length - 1, 1 );
                andM.Remove( andM.Length - 1, 1 );
                and.Remove( and.Length - 1, 1 );
                txtOr.Text = orm.ToString() + "\t" + or.ToString();
                txtAnd.Text = andM.ToString() + "\t" + and.ToString();

            }
            catch ( ExpressionException value )
            {
                ShowValidate( value );
            }
        }

    }
}
